﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;

/// <summary>
/// 获取商家的订单列表
/// </summary>
public partial class AndroidAPI_user_GetOrderListByUserId : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();

        Custorder dal = new Custorder();

        int pagesize = HjNetHelper.GetPostParam("pagesize", 8);
        int pageindex = HjNetHelper.GetPostParam("pageindex", 1);
        int pagecount = 1;

        string order_state = WebUtility.InputText(Request["state"]);//订单状态
        string did = WebUtility.InputText(Request["did"]);//商家编号


        StringBuilder orderlistjson = new StringBuilder();

        IList<CustorderInfo> list = new List<CustorderInfo>();

        string SqlWhere = "  Custorder.delflag= 0 and  userid=" + did;

        if (order_state != "" && order_state != "-1")
        {
            SqlWhere += " and OrderStatus=" + order_state;
        }

        string searchkey = WebUtility.InputText(Request["searchkey"]);//订单编号，或者电话
        if (searchkey != null && searchkey.Length > 0)
        {
            SqlWhere += " and ( orderid like '%"+searchkey+ "%' or OrderComm like '%" + searchkey + "%') ";
        }

        int count = 0;

        count = dal.GetCount(SqlWhere);
        list = dal.GetList(pagesize, pageindex, SqlWhere, "OrderDateTime", 1);

        if (count % pagesize == 0)//整数倍
        {
            pagecount = count / pagesize;
        }
        else
        {
            pagecount = count / pagesize + 1;
        }

        orderlistjson.Append("{\"page\":\"" + pageindex.ToString() + "\",\"total\":\"" + pagecount + "\", \"orderlist\":[");

        CustorderInfo info = new CustorderInfo();

        IList<DeliverInfo> delivers = SectionProxyData.GetDeliverList();

        for (int i = 0; i < list.Count; i++)
        {
            info = new CustorderInfo();
            info = list[i];
            orderlistjson.Append("{\"orderid\":\"" + info.orderid + "\",\"shopname\":\"" + info.TogoName + "\",\"addtime\":\"" + info.OrderDateTime.ToString("yyyy-MM-dd HH:mm:dd") + "\",\"totalmoney\":\"" + info.OrderSums + "\",\"state\":\"" + info.OrderStatus.ToString() + "\",\"address\":\"" + info.OrderAddrEx.ToString() + "\"");
            orderlistjson.Append(",\"PayMode\":\"" + info.paymode + "\"");
            orderlistjson.Append(",\"paystate\":\"" + info.paystate + "\"");
            orderlistjson.Append(",\"sendstate\":\"" + info.sendstate + "\"");
            orderlistjson.Append(",\"eattype\":\"" + info.ReveInt2 + "\"");
            orderlistjson.Append(",\"IsShopSet\":\"" + info.IsShopSet + "\"");
            orderlistjson.Append(",\"SendTime\":\"" + info.SendTime + "\"");
            orderlistjson.Append(",\"people\":\"" + info.ReveInt1 + "\"");

            string delivername = "";
            string deliverphone = "";

            foreach (var item in delivers)
            {
                if (item.DataId == info.deliverid)
                {
                    delivername = item.Name;
                    deliverphone = item.Phone;
                    break;
                }
            }


            orderlistjson.Append(",\"delivername\":\"" + delivername + "\"");
            orderlistjson.Append(",\"deliverphone\":\"" + deliverphone + "\"");


            orderlistjson.Append(",\"SystemUserId\":\"" + info.SystemUserId + "\"");

            orderlistjson.Append(",\"realname\":\"" + info.OrderRcver + "\"");
            orderlistjson.Append(",\"phone\":\"" + info.OrderComm + "\"");
            orderlistjson.Append(",\"sendmoney\":\"" + info.shopdiscountmoney + "\"");
            orderlistjson.Append("},");
        }

        orderlistjson.Append("]}");

        Response.Write(orderlistjson.ToString().Replace("},]}", "}]}"));
        Response.End();

    }
}
